package com.china.zhuoyue.ui.activity.main

import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.china.zhuoyue.R
import com.china.zhuoyue.databinding.ActivityMainBinding
import com.china.zhuoyue.ui.base.BaseActivity
import com.china.zhuoyue.ui.ext.getString
import com.china.zhuoyue.ui.ext.tabData
import com.china.zhuoyue.ui.fragment.BusinessAreaFragment
import com.china.zhuoyue.ui.fragment.IndexFragment
import com.china.zhuoyue.ui.fragment.MineFragment
import com.china.zhuoyue.ui.utils.FragmentChangeManager
import com.china.zhuoyue.ui.utils.Utils
import com.china.zhuoyue.ui.viewmodel.MainViewModel
import com.flyco.tablayout.listener.OnTabSelectListener
import com.uphone.kingmall.main.HomeFragment
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.util.*


class MainActivity : BaseActivity<ActivityMainBinding>(), OnTabSelectListener {
    private val mMainViewModel: MainViewModel by viewModels()

    private var mFragmentChangeManager: FragmentChangeManager? = null
    override fun initView() {
        Utils.invasionStatusBar(this)
        vb.tabLayout.tabData {
            tab(R.string.text_main_nav_1.getString(), R.mipmap.icon_main_nav_index, R.mipmap.icon_main_nav_un_index)
            tab(R.string.text_main_nav_2.getString(), R.mipmap.icon_main_nav_mall, R.mipmap.icon_main_nav_un_mall)
//            tab(R.string.text_main_nav_3.getString(), R.mipmap.icon_main_nav_index, R.mipmap.icon_main_nav_index)
            tab(R.string.text_main_nav_4.getString(), R.mipmap.icon_main_nav_loc, R.mipmap.icon_main_nav_un_loc)
            tab(R.string.text_main_nav_5.getString(), R.mipmap.icon_main_nav_mine, R.mipmap.icon_main_nav_un_mine)
        }
        vb.tabLayout.setOnTabSelectListener(this)
        val fragments = ArrayList<Fragment>()
        fragments.add(IndexFragment())
        fragments.add(HomeFragment())
        fragments.add(BusinessAreaFragment())
        fragments.add(MineFragment())
        mFragmentChangeManager = FragmentChangeManager(supportFragmentManager, R.id.frame_layout, fragments)
        mMainViewModel.changeTabLiveData.observe(this) {
            vb.tabLayout.currentTab = it
            onTabSelect(it)
        }

        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                mMainViewModel.mu.collect {

                }
            }
        }
    }

    override fun onTabSelect(position: Int) {
        mFragmentChangeManager?.setFragments(position)
    }

    override fun onTabReselect(position: Int) {

    }

}